<?php 
## include required files
/*******************************/
require_once '../model/trade.php';
/*******************************/

## Create Objects
/*******************************/
$tradeObj = new Model_TRADE();
/*******************************/

$trade_id = base64_decode($_GET['id']);

if(isset($_POST['edittrade_btn'])) 
{
	## apply PHP validation for required filed
	if(trim($_POST['cat_name']) != '') 
	{
		extract($_POST);
		$tradeArray['trade_category']   = return_post_value($cat_name);
		$tradeArray['description']   = return_post_value($description);
		
		if($_GET['pid']!='')
		{
                        $is_subcat = true;
                        $expired_time = getArrayExpiredTime();
                        $tradeArray['price'] = $_POST['cat_price'];
                        $tradeArray['expired_time'] = $_POST['cat_expired_time'];
                        $smarty->assign('expired_time', $expired_time);
			$tradeArray['parent_id']   = base64_decode($_GET['pid']);
		}
		else
		{
                        $is_subcat = false;
			$tradeArray['parent_id']   = 0;
		}
		$tradeArray['status']   = 1;
		
		if($_GET['pid']!='')
		{
			$getdetailjobtype = $tradeObj->checkJobTypeName($_POST['cat_name'], $trade_id);
			if($getdetailjobtype['id']=='')
			{
				$tradeObj->editTradeCategoryById($tradeArray, $trade_id);
				$_SESSION['msg'] = "<div class='success_msg'><span>Job Type edited successfully</span></div>";
				header('location:'.SITE_URL.'/admin/home.php?q=tradecategories&pid='.$_GET["pid"].'&page='.$_POST['page']);
				exit;
			}			
			else
			{
				$_SESSION['msg'] = "<div class='error_msg'><span>Job Type Name already used</span></div>";				
			}
		} 
		else 
		{
			$tradeObj->editTradeCategoryById($tradeArray, $trade_id);
			$_SESSION['msg'] = "<div class='success_msg'><span>Trade category edited successfully</span></div>";
			header('location:'.SITE_URL.'/admin/home.php?q=tradecategories&page='.$_POST['page']);
			exit;
		}
		
	} else { // php validation else
		$_SESSION['msg'] = '<div class="error_msg"><span>Please enter trade category</span></div>';
	}
	// main if closed
}

## Fetch trade category by id
if($_GET['id'] != ''&& $_GET['action'] == 'edit') {
	
	$smarty->assign('page', $_GET['page']);
	$tradecatArray = $tradeObj->getTradeCategoryDetailsById($trade_id);
	

	$smarty->assign('tradecatArray', $tradecatArray);
}
if($_GET['pid']!='')
{
        $is_subcat = true;
        $expired_time = getArrayExpiredTime();
        $smarty->assign('expired_time', $expired_time);
	$pid= base64_decode($_GET['pid']);
	$parentArray=$tradeObj->getTradeCategoryDetailsById($pid);
	$smarty->assign('parentArray', $parentArray);
	//echo '<pre>';print_r($moduelArray);exit;
}else{
    $is_subcat = false;
}
$smarty->assign('is_subcat', $is_subcat);

## Assign success or error msg to smarty variable and unset session variable
if(trim($_SESSION['msg']) != '') {
	$smarty->assign('msg', $_SESSION['msg']);
	unset($_SESSION['msg']);
}

## Set active class variable for left menu
$smarty->assign('activeclass', 'tradecat');
$smarty->assign('mainmenu', '2');
## Unset all the objects created which are on this page
unset($tradeObj);

$smarty->display(TEMPLATEDIR_ADMIN. 'tradecategories/edittradecategory.tpl');
unset($smarty);
?>